﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.SoldierArchives.BLL;
using Yipusoft.SoldierArchives.Model;
using Yipusoft.Common.BLL;
using Yipusoft.Account.Model;
using Yipusoft.Account.BLL;
using Yipusoft.Common.Model;
using Yipusoft.TranslateApprove.BLL;
using Yipusoft.TranslateApprove.Model;
using System.Collections;

public partial class Police_TranslateProcess_TranslateInfo : System.Web.UI.Page
{
    public String InfoStr = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Bind();
        }
    }

    void Bind()
    {
        BindRoles();
        if (Request["SelectId"] != null)
        {
            String[] SIDArr = Request["SelectId"].ToString().Substring(1).Split(',');
            ArrayList InfoArr = new ArrayList();
            for (int i = 0; i < SIDArr.Length; i++)
            {
                Yipusoft.TranslateApprove.DAL.SelectDataContext db = new Yipusoft.TranslateApprove.DAL.SelectDataContext();
                Yipusoft.TranslateApprove.DAL.TranslateSelect select = db.TranslateSelect.Where(o => o.Id == new Guid(SIDArr[i])).FirstOrDefault();
                SoldierName.Text = select.SoldierName;
                Sex.Text = select.Sex;
                Rank.Text = select.Rank == "" ? "" : CodeItem.GetCodeItem(select.Rank).ItemName;
                DateArmy.Text = select.JoinTime.Value.Year == 1 ? "" : select.JoinTime.Value.ToShortDateString();
                JingZhongId.Text = select.FromJingZhong;
                JingZhong.Text = CodeItem.GetCodeItem(select.FromJingZhong).ItemName;
                OldDepartment.Text = select.OldDepartment;
                OldDepartment2.Text = select.OldDepartment2;

                TranslateInfo info = new TranslateInfo();
                info.DateArmy = Convert.ToDateTime(DateArmy.Text);
                info.DateLastModefy = DateTime.Now;
                info.NewDepartment = txtNewDepartment.Text;
                info.OldDepartment = OldDepartment.Text;
                info.OldDepartment2 = OldDepartment2.Text;
                info.Ranker = Rank.Text;
                info.Sex = Sex.Text;
                info.SoldierName = SoldierName.Text;
                info.TID = 1;
                info.SID = Guid.Empty;
                info.FromDepartmentID = "";
                info.FromJingZhong = JingZhongId.Text;
                info.ToDepartmentID = "1300000";
                info.ToJingZhong = "SBDD30";

                InfoStr += "<tr>"+
                        "<td height=\"25\" valign='top'>" + info.SoldierName + "</td>" +
                        "<td valign='top'>" + info.Sex + "</td>" +
                        "<td valign='top'>" + info.Ranker + "</td>" +
                        "<td valign='top'>" + DateArmy.Text + "</td>" +
                        "<td valign='top'>" + OldDepartment.Text + "</td>" +
                        "<td valign='top'>河北省公安消防总队</td>" +
                        "<td valign='top'>" + info.OldDepartment + "</td>" +
                        "<td valign='top'>" + info.OldDepartment2 + "</td>" +
                      "</tr>";

               
                InfoArr.Add(info);
            }
            Session["TranslateInfo"] = InfoArr;
        }
    }

    private void BindRoles()
    {
        ddlRoles.DataSource = Process.GetJumpList(1, 0);
        ddlRoles.DataBind();
    }

    public List<DepartmentInfo> GetDepartmentList()
    {
        List<DepartmentInfo> departmentList = new List<DepartmentInfo>();
        departmentList = Department.GetList("Right(DepartmentID,2)='00'");
        return departmentList;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {

    }
    protected void btnOk_Click(object sender, EventArgs e)
    {

        #region 验证
        DateTime dateArmy = DateTime.MinValue;
        if (!DateTime.TryParse(DateArmy.Text, out dateArmy))
        {
            Prompt.Text = "提示：入伍时间错误!";
            return;
        }

        string ex = "";
        if (!WordFile.HasFile)
        {

        }
        else
        {

            ex = WordFile.FileName.Substring(WordFile.FileName.LastIndexOf('.') + 1);
            if (ex.ToLower() != "doc" && ex.ToLower() == "jpg" && ex.ToLower() == "gif" && ex.ToLower() == "png" && ex.ToLower() == "bmp")
            {
                Prompt.Text = "提示:上传必须为Word文档或图片!";
                return;
            }
            if (WordFile.FileContent.Length / 1024 / 1024 > 5)
            {
                Prompt.Text = "提示:上传文档不可大于5M!";
                return;

            }
        }
        #endregion

        #region 添加士兵基本信息
        ArrayList InfoArr = (ArrayList)Session["TranslateInfo"];
        Guid Fid=new Guid();
        TranslateLogInfo log;
        #region 上传请示文件

        Guid Aid = Guid.Empty; //请示文件ID
        string path = Server.MapPath("..\\TranslateApprove\\WordFile\\");

        ApplyInfo apply = new ApplyInfo();

        if (!WordFile.HasFile)
        {

        }
        else
        {
            string word = DateTime.Now.ToString("yyyyMMddhhmmssfff") + "." + ex;

            WordFile.SaveAs(path + word);
            apply.FilePath = word;
        }

        apply.ATime = DateTime.Now;
        apply.Title = ApplyTitle.Text;
        apply.UserID = User.Identity.Name;
        Aid = Apply.Insert(apply);
        #endregion
        for (int i = 0; i < InfoArr.Count; i++)
        {
            TranslateInfo info =(TranslateInfo)InfoArr[i];
            //info.DateArmy = Convert.ToDateTime(DateArmy.Text);
            //info.DateLastModefy = DateTime.Now;
            //info.NewDepartment = txtNewDepartment.Text;
            //info.OldDepartment = OldDepartment.Text;
            //info.OldDepartment2 = OldDepartment2.Text;
            //info.Ranker = Rank.Text;
            //info.Sex = Sex.Text;
            //info.SoldierName = SoldierName.Text;
            //info.TID = 1;
            //info.SID = Guid.Empty;
            //info.FromDepartmentID = "";
            //info.FromJingZhong = JingZhongId.Text;
            //info.ToDepartmentID = "1300000";
            //info.ToJingZhong = "SBDD30";
            Fid = Translate.Insert(info);
            log = new TranslateLogInfo();
            log.FID = Fid;
            log.PID = 1;
            log.LastTime = DateTime.Now;
            TranslateLogs.Insert(log);
            
            //调动流程
            TranslateInfo TranslateInfo = Translate.GetDetail(Fid);
            log = new TranslateLogInfo();
            log.DID = Aid;
            log.FID = TranslateInfo.ID;
            log.LastTime = DateTime.Now;
            if (TranslateLogs.GetLastDetail(TranslateInfo.ID) != null)
            {
                log.PID = TranslateProcess.GetNextDetail(TranslateLogs.GetLastDetail(TranslateInfo.ID).PID).ID;

            }
            else
            {


                log.PID = TranslateProcess.GetFirstDetail(TranslateInfo.TID).ID;
            }
            TranslateLogs.Insert(log);
            #region 提交与相应的角色


            try
            {
                int PID = Convert.ToInt32(ddlRoles.SelectedValue.ToString());
                Yipusoft.TranslateApprove.Model.ProcessInfo process = Process.GetModel(PID);
                SetupLogInfo setup = new SetupLogInfo();
                setup.FID = Aid;
                setup.KID = 1;
                setup.PID = Process.GetModel(1, 0).PID;
                setup.Remark = "";
                setup.RTime = DateTime.Now;
                setup.State = 1;
                setup.UserID = User.Identity.Name;
                setup.UserName = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).UserName;
                SetupLog.Add(setup);
                string where = " Kid=1 and orderid>0 and orderid<" + process.OrderID + "  ";
                List<Yipusoft.TranslateApprove.Model.ProcessInfo> list = Process.GetList(where);
                foreach (Yipusoft.TranslateApprove.Model.ProcessInfo aProcess in list)
                {
                    //请示流程
                    SetupLogInfo setup1 = new SetupLogInfo();
                    setup1.FID = Aid;
                    setup1.KID = 1;
                    setup1.PID = aProcess.PID;
                    setup1.Remark = "";
                    setup1.RTime = DateTime.Now;
                    setup1.State = 4;
                    setup1.UserID = User.Identity.Name;
                    setup1.UserName = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).UserName;
                    SetupLog.Add(setup1);
                }
            }
            catch { }


            #endregion
            #region 士兵选择添加日志

                Yipusoft.TranslateApprove.DAL.SelectDataContext db = new Yipusoft.TranslateApprove.DAL.SelectDataContext();
                Yipusoft.TranslateApprove.DAL.TranslateSelect select = db.TranslateSelect.Where(o => o.Id == info.ID).FirstOrDefault();
                //Yipusoft.TranslateApprove.DAL.SelectLogs Selectlog = new Yipusoft.TranslateApprove.DAL.SelectLogs() { Id = Guid.NewGuid(), SelectId = select.Id, SetDate = DateTime.Now, SetUserId = User.Identity.Name, State = 3 };
                //db.SelectLogs.InsertOnSubmit(Selectlog);
                db.SubmitChanges();
            #endregion
        }
        
        //Response.Redirect("ProcessList.aspx");
        #endregion

        

        

        


        Response.Redirect("ProcessList.aspx");
    }
}
